/***************************************
 * 说明：本文件主要列举第4章文中所实现的例题
 * *************************************/
#include "main.h"
#include<string.h>
/******************************************
 * 例子4.1：通过‘+’实现两个复数相加
 * ****************************************/
Complex::Complex(double re, double im)
{
    real = re;
    imag = im;
}
Complex Complex::comp_add(Complex& com)
{
    Complex result;
    result.real = real + com.real;
    result.imag = imag + com.imag;
    return result;
}
void example1()
{
    Complex comp1(1,2);
    Complex comp2(3,4);
    Complex comp3;
    comp3 = comp1.comp_add(comp2);
    comp3.display();
}
/******************************************
 * 例子4.2：重载‘+’运算符，实现两个复数的相加
 * ****************************************/
Complex Complex::operator+(Complex& com)
{
    Complex result;
    result.real = real + com.real;
    result.imag = imag + com.imag;
    return result;
}
void Complex::display()
{
    cout << "(" << real << "," << imag << ")" << endl;
}
void example2()
{
    Complex com1(1,2);
    Complex com2(3,4);
    Complex comp3;
    comp3 = com1 + com2;
    comp3.display();
}
/******************************************
 * 例子4.3：重载‘+’运算符，实现两个复数的相加，
 * ‘+’运算符作为类Complex的友元函数
 * ****************************************/
Complex1::Complex1(double re, double im)
{
    real = re;
    imag = im;
}
Complex1 operator+(Complex1& comp1, Complex1& comp2)
{
    return Complex1(comp1.real + comp2.real, comp1.imag + comp2.imag);
}
void Complex1::display()
{
    cout << "(" << real << "," << imag << ")" << endl;
}
void example3()
{
    Complex1 com1(1,2);
    Complex1 com2(3,4);
    Complex1 comp3;
    comp3 = com1 + com2;
    com1.display();
    com2.display();
    comp3.display();
}
/******************************************
 * 例子4.4：声明一个String类，重载运算符‘==’ ‘>’
 * '<'使得该类可以用于两个字符串的等于，大于，小于
 * ****************************************/
String::String()
{
    p = nullptr;
}
String::String(char* str)
{
    p = str;
}
void String::display()
{
    cout << p;
}
void example4()
{
    char ch1[] = "wang";
    char ch2[] = "dongfang";
    String str1(ch1);
    str1.display();
    String str2(ch2);
    str2.display();
}
bool operator>(String& str1, String& str2)
{
    return (strcmp(str1.p, str2.p) > 0) ? true : false;
}
bool operator<(String& str1, String& str2)
{
    return (strcmp(str1.p, str2.p) < 0) ? true : false;
}
bool operator==(String& str1, String& str2)
{
    return (strcmp(str1.p, str2.p) == 0) ? true : false;
}
void example42()
{
    char ch1[] = "wang";
    char ch2[] = "dongfang";
    String str1(ch1);
    String str2(ch2);
    cout << (str1 > str2) << endl;
    cout << (str1 < str2) << endl;
    cout << (str1 == str2) << endl;
}
/*****************************
 * 例子4.4：进一步完善上述程序
 * ***************************/
void compare(String& str1, String& str2)
{
    if (operator>(str1, str2)) {
        str1.display();
        cout << ">";
        str2.display();
    } else if (operator<(str1, str2)) {
        str1.display();
        cout << "<";
        str2.display();
    } else {
        str1.display();
        cout << "==";
        str2.display();
    }
    cout << endl;
}
void example43()
{
    char ch1[] = "wang";
    char ch2[] = "dongfang";
    String str1(ch1);
    String str2(ch2);
    compare(str1, str2);
}
/***********************************************
 * 例子4.5：有一个Time类，包含数据成员minute/second，
 * 模拟秒表 * 每60秒，分钟加1，秒数归0
 * *********************************************/
Time::Time()
{
    minute = 0;
    second = 0;
}
Time::Time(int m, int s)
{
    minute = m;
    second =s;
}
Time Time::operator++()
{
    if (++second >= 60) {
        minute++;
        second = second - 60;
    }
    return *this;
}
Time Time::operator++(int)
{
    second++;
    if (second >= 60) {
        second -= 60;
        minute++;
    }
    return *this;
}
void Time::display()
{
    cout << minute << ":" << second << endl;
}
void example5()
{
    Time time(24,0);
    for (int i = 0; i<61; i++) {
        time++;
        time.display();
    }
}
/***********************************************
 * 例子4.7：重载<<，用于输出复数
 * *********************************************/
ostream& operator<<(ostream& output, Complex& complex)
{
    output << "(" << complex.real << "," << complex.imag << ")" << endl;
    return output;
}
istream& operator>>(istream& input, Complex& complex)
{
    input >> complex.real >> complex.imag;
    return input;
}
void example7()
{
    Complex comp1(1,2), comp2(3,4);
    Complex comp3 = comp1 + comp2;
    cout << comp3;
    Complex comp4;
    cin >> comp4;
    cout << comp4;
}
int main()
{
    example7();
}